package liuliu.throughwaring.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import net.tsz.afinal.annotation.view.CodeNote;

import java.util.List;

import liuliu.custom.control.dialog.LLDialog;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.adapter.CommonAdapter;
import liuliu.throughwaring.adapter.ViewHolder;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.add.police.company.IRegUnitView;
import liuliu.throughwaring.control.task.addTask;
import liuliu.custom.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.ui.map.MultipleRoutePlanningActivity;

/**
 * 采集信息列表
 * Created by Administrator on 2016/2/26.
 */
public class NewCompanyListActivity extends BaseActivity implements IRegUnitView {
    @CodeNote(id = R.id.new_company_toolbar)
    TToolbar title;
    @CodeNote(id = R.id.new_company_list)
    ListView lv;
    List<CompanyModel> list;
    List<CodeModel> mCodeList;
    LLDialog llDialog;//提示是否接收企业信息
    addTask add;
    CompanyModel cm;
    @CodeNote(id = R.id.btn_weijieshou)
    ImageButton btnWei;
    @CodeNote(id = R.id.btn_yijieshou)
    ImageButton btnYi;
    String pid = "";
    @CodeNote(id = R.id.new_company_mes)
    TextView mes;


    @Override
    public void initViews() {
        setContentView(R.layout.activity_new_company_list);
        add = new addTask(this);
        mUtils = new Utils(this);
        mCodeList = finalDb.findAllByWhere(CodeModel.class, "codeName='Code_CompanyType'");
        list = null;
        pid = Utils.ReadString("PoliceId");
        list = finalDb.findAllByWhere(CompanyModel.class, "IsPush='true' and CompanyType='Company' order by CompanyCreateTime");//推送
        btnWei.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = null;
                list = finalDb.findAllByWhere(CompanyModel.class, "IsPush='true' and CompanyType='Company' order by CompanyCreateTime");//推送
                initEvents();
                btnWei.setBackgroundResource(R.mipmap.wei__lan);
                btnYi.setBackgroundResource(R.mipmap.yi_hui);
            }
        });
        btnYi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list = null;
                if (!pid.equals("")) {
                    list = finalDb.findAllByWhere(CompanyModel.class, "CompanyPoliceId='" + pid + "' and IsPush ='false' and CompanyType='Company' order by CompanyChangeTime");
                    initEvents();
                } else {
                    list = null;
                }
                btnWei.setBackgroundResource(R.mipmap.wei_hui);
                btnYi.setBackgroundResource(R.mipmap.yi__lan);
            }
        });
        title.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                finish();
            }
        });
    }

    @Override
    public void initEvents() {
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                if (list.get(position).getIsPush().equals("false")) {
                    intent.setClass(NewCompanyListActivity.this, RegUnitActivity.class);
                    bundle.putSerializable("UnitModel", list.get(position));
                    intent.putExtra("type", "Company");
                    intent.putExtras(bundle);
                    startActivity(intent);
                    finish();
                } else {
                    intent.setClass(NewCompanyListActivity.this, UnitDetailActivity.class);
                    bundle.putSerializable("CompanyModel", list.get(position));
                    intent.putExtras(bundle);
                    startActivity(intent);
                }

            }
        });
        if (null != list && list.size() != 0) {
            lv.setAdapter(new CommonAdapter<CompanyModel>(this, list,
                    R.layout.item_company_tuisong) {
                @Override
                public void convert(final ViewHolder holder, final CompanyModel model, int position) {
                    holder.setText(R.id.xuhao_tv, (position + 1) + "");
                    if (model.getCompanyName().length() > 14) {
                        holder.setText(R.id.unit_search_item_main_unit_name, model.getCompanyName().substring(0, 14) + "...");
                    } else {
                        holder.setText(R.id.unit_search_item_main_unit_name, model.getCompanyName());
                    }
                    holder.setOnClickListener(R.id.unit_search_tel, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + model.getCompanyPhoneNumber()));
                            startActivity(intent);
                        }
                    });
                    holder.setText(R.id.unit_search_item_main_tel, model.getCompanyPhoneNumber());
                    holder.setText(R.id.unit_search_item_main_address, model.getCompanyAddress());
                    holder.setOnClickListener(R.id.tuisong_btn_daohang, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(NewCompanyListActivity.this, MultipleRoutePlanningActivity.class);
                            startActivity(intent);
                        }
                    });
                    holder.setOnClickListener(R.id.tuisong_recive, new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (model.getIsPush().equals("true")) {
                                llDialog = new LLDialog(NewCompanyListActivity.this);
                                llDialog.setMiddleMessage("确认要接收企业信息？");
                                llDialog.setOnPositiveListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        llDialog.dismiss();
                                        //改变状态，修改企业信息
                                        cm = model;
                                        cm.setIsPush("false");
                                        add.addCompany(cm, NewCompanyListActivity.this);
                                    }
                                });
                                llDialog.show();
                                llDialog.setOnNegativeListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        llDialog.dismiss();
                                    }
                                });
                            } else {//拨打电话操作
                                Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + model.getCompanyPhoneNumber()));
                                startActivity(intent);
                            }
                        }
                    });
                    if (model.getIsPush().equals("true")) {
                        holder.setText(R.id.tuisong_recive, "接收");
                    }
                }
            });
            mes.setVisibility(View.GONE);
            lv.setVisibility(View.VISIBLE);
        } else {
            mes.setVisibility(View.VISIBLE);
            lv.setVisibility(View.GONE);
        }
    }

    @Override
    public void onRegUnitResult(boolean isTrue, String mes) {
        if (isTrue) {
            mUtils.ToastShort("接收成功");
            list = null;
            list = finalDb.findAllByWhere(CompanyModel.class, "IsPush='true' and CompanyType='Company' order by CompanyCreateTime");//推送
            initEvents();
        } else {
            mUtils.ToastShort("接收失败");
        }
    }
}
